package com.mycompany.project.client;

import com.extjs.gxt.ui.client.widget.ProgressBar;
//fsdfd
import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.Context2d.TextAlign;
import com.google.gwt.canvas.dom.client.Context2d.TextBaseline;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.AbsolutePanel;

/**
 * @author Laboratorio Tv Collaborativa/Contenuti Digitali --> Sardegna District --> Crs4 --> Sardegna Ricerche
 * 
 * This class extends {@link AbsolutePanel} and implements the @SaStria video progress Bar.
 * This custom progress bar differ by standard progress bar in some particularity as
 * the management and implementation of MouseHandler events and the implementation of
 * draggable cursor. 
 */
public class MyProgressBar extends AbsolutePanel {
	private Canvas canvasPBar;   // canvas object where draw a draggable video cursor
	private ProgressBar progressBarVideo; // Standard progress Bar to overwrite.
	private Context2d canvasDraw;    // Context2d used to draw on canvasPBar canvas object
	private double largezzaProgress;  // progress bar width
	private int heigthPBar; // progress bar height
		
	
	/**
	 * Constructor of this class. Its create a new custom progress bar.
	 */
	public MyProgressBar() {
		progressBarVideo=new ProgressBar();  // create a standard progress bar
		canvasPBar=Canvas.createIfSupported();	 // create a drawable canvas object.	
		add(progressBarVideo,0,0);  // add/draw standard progress bar on this custom progress bar
		add(canvasPBar,0,0);   // add/draw canvas object on this custom progress bar
		canvasPBar.setSize("100%","100%");  // sets to fill 100% size of custom progress bar 
		progressBarVideo.setSize("100%","100%"); // sets to fill 100% size of custom progress bar
		canvasDraw=canvasPBar.getContext2d(); // gets canvas object drawable tool. 
		
		/*
		 * Sets Canvas text font of progress bar Text
		 */
		canvasDraw.setTextBaseline(TextBaseline.ALPHABETIC);	 	
		canvasDraw.setTextAlign(TextAlign.START);		
		canvasDraw.setFont("bold 90px Unknown Font, sans-serif");		
		largezzaProgress=canvasPBar.getOffsetWidth();  //sets progress bar width		
		
		
		//Window.alert("Larghezza progress canvas: " + largezzaProgress + "Larghezza Bar: " + progressBarVideo.getWidth() + "/" + progressBarVideo.getOffsetWidth());
		
	}
	
	
	/**
	 * Update progress bar progress value. The range value are 0(0%) - 1(100%). 
	 * @param value double value between 0/1 range where 0=0% progress, 1=100% 
	 * progress. 
	 * @param text Text string to write on progress Bar
	 */
	public void updateProgress(double value, String text){
		progressBarVideo.updateProgress(value, text); // update progress
		
		/*
		 * Draw a draggable cursor on progress bar. The cursor is composed of
		 * two circle one inside the other.
		 */
		canvasDraw.setStrokeStyle("red");  // set cursor color
		canvasDraw.clearRect(0, 0, canvasPBar.getOffsetWidth(), canvasPBar.getOffsetWidth()); // clear the old possible cursor
		canvasDraw.beginPath();    // start the draw path of first cursor circle. the internal circle
		canvasDraw.setLineWidth(3); // set marking pen size
		canvasDraw.arc((value*largezzaProgress), heigthPBar/2 , 5, 0, 360);  // draw on current path the internal circle
		canvasDraw.stroke();    // draws physically the current path. All pending changes
		canvasDraw.closePath(); // close first drawing circle path
		canvasDraw.beginPath();  // start the draw path of second cursor circle. the external circle
		canvasDraw.arc((value*largezzaProgress), heigthPBar/2 , 10, 0, 360); // draw on current path the external circle	
		canvasDraw.stroke();  // draws physically the current path. All pending changes
		canvasDraw.closePath();	// close drawing circle path
	}
	
	
	/**
	 * Update progress bar caption text
	 * @param text. String to set in progress bar caption
	 */
	public void updateText(String text){
		progressBarVideo.updateText(text);
	}
	
	
	/**
	 * Set the media player custom progress bar position on @SaStria tool.
	 * @param left: integer value of left side position
	 * @param top : integer value of top side position
	 */
	public void setPosition(int left, int top){
		progressBarVideo.setPosition(left, top);
	}

	
	/**
	 * Set the media player custom progress bar width on @SaStria tool.
	 * @param offsetWidth : integer value of media player progress bar width.
	 */
	public void setWidth(int offsetWidth) {
		this.setWidth(offsetWidth+"px");  // set custom progress bar width
		progressBarVideo.setWidth(offsetWidth); // set progress bar width according to this custom progress bar width
	   	canvasPBar.setWidth(offsetWidth+"px"); //  set canvas for drawing draggable cursor width according to this custom progress bar width
	   	canvasPBar.setCoordinateSpaceHeight(canvasPBar.getOffsetHeight());  // set canvas for drawing draggable cursor Coordinate Space Height according to progress bar size
	   	canvasPBar.setCoordinateSpaceWidth(offsetWidth); // set canvas for drawing draggable cursor Coordinate Space Width according to progress bar size
	   	largezzaProgress=canvasPBar.getOffsetWidth(); // Save progress bar width.
	   	heigthPBar=canvasPBar.getOffsetHeight(); // Save progress bar height.
	}
	
	
	/**
	 * Adds a {@link MouseMoveHandler} handler.
	 * @param handler: the mouse move handler
	 * @return {@link HandlerRegistration} used to remove this handler
	 */
	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler){
		return addDomHandler(handler, MouseMoveEvent.getType());		
	}
	
	
	/**
	 * Adds a {@link MouseDownHandler} handler.
	 * @param handler: the mouse Down handler
	 * @return {@link HandlerRegistration} used to remove this handler
	 */
	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler){
		return addDomHandler(handler, MouseDownEvent.getType());		
	}

	
	/**
 	 * Adds a {@link MouseUpHandler} handler.
	 * @param handler: the mouse up handler
	 * @return {@link HandlerRegistration} used to remove this handler
	 */
	public HandlerRegistration addMouseUpHandler(MouseUpHandler handler){
		return addDomHandler(handler, MouseUpEvent.getType());		
	}
	
	
	
	/**
	 * Adds a {@link MouseOutHandler} handler.
	 * @param handler: the mouse move handler
	 * @return {@link HandlerRegistration} used to remove this handler
	 */
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler){
		return addDomHandler(handler, MouseOutEvent.getType());			
	}
		
	
	/**
	 * Adds a {@link MouseOverHandler} handler.
	 * @param handler: the mouse over handler
	 * @return {@link HandlerRegistration} used to remove this handler
	 */
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler){
		return addDomHandler(handler, MouseOverEvent.getType());
	}
	
	
	
	/**
	 * Adds a {@link MouseWheelHandler} handler.
	 * @param handler: the mouse wheel handler
	 * @return {@link HandlerRegistration} used to remove this handler
	 */
	public HandlerRegistration addMouseWhellClickHandler(MouseWheelHandler handler){
		return addDomHandler(handler, MouseWheelEvent.getType());
	}
		
}
